﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace WMP
{
    class ConvertXML
    {
        public void ListToXml(List<ObjListe> myItems, string name)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<ObjListe>));
            //System.IO.File.Create(@"./PlaylistsXML\" + name + ".xml");
            TextWriter textWriter = new StreamWriter(@"./PlaylistsXML\" + name + ".xml");
            serializer.Serialize(textWriter, myItems);
            textWriter.Close();
        }

        public void BiblioToXml(List<ObjListe> myItems)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<ObjListe>));
            TextWriter textWriter = new StreamWriter(@"./biblio.xml");
            serializer.Serialize(textWriter, myItems);
            textWriter.Close();
        }

        public List<ObjListe> XmlToList(string path)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<ObjListe>));
            TextReader textReader = new StreamReader(@path);
            List<ObjListe> XmlitemList = new List<ObjListe>();
            XmlitemList = (List<ObjListe>)deserializer.Deserialize(textReader);
            textReader.Close();
            /*foreach (Item x in XmlitemList)
            {
                Console.WriteLine(x.name);
                Console.WriteLine(x.type);
            }*/
            return XmlitemList;
        }

        public List<ObjListe> XmlToBiblio()
        {
            string path = @"./biblio.xml";
            List<ObjListe> XmlitemList = new List<ObjListe>();
            if (File.Exists(path))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(List<ObjListe>));
                TextReader textReader = new StreamReader(path);
                XmlitemList = (List<ObjListe>)deserializer.Deserialize(textReader);
                textReader.Close();
            }
            return XmlitemList;
        }
    }
}
